﻿using AngleSharp.Html.Parser;
using Core.Expansion;
using SuperSocket.ClientEngine;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Tool;
using Tool.Common;
using Tool.Web.HttpHelper;
using static Core.Common.Global;

namespace Core.Function
{
	static class Pixiv
	{
		static HtmlParser parser = new HtmlParser();
		static string[] urls = new string[] { "http://api.mtyqx.cn/api/random.php", "http://www.dmoe.cc/random.php", "https://www.320nle.com/acggirl/acgurl.php" };
		static Random random = new Random();
		static string[] urls2 = new string[] { "https://img.xjh.me/random_img.php", "http://myepk.club:2020/?msg=ecy", "https://random.52ecy.cn/randbg.php" };
		
		internal static string GetRandImg()
		{
			var data = HttpNet.Get(urls[random.Next(3)]);
			if (data.Length == 0)
			{
				return "获取失败";
			}
			else
			{
				//var html = parser.ParseDocument(Encoding.UTF8.GetString(data));
				//var img = html.QuerySelector("img");
				var path = ImagePath+ TimeHelp.GetCurrentTimeUnix(true) + ".jpg";
				//File.WriteAllBytes(path, HttpNet.Get(img.Attributes["src"].Value));
				File.WriteAllBytes(path, data);
				return CQ.Code_Image(path.HandleImagePath());
			}
		}

		internal static string GetRandCos()
		{
			//https://moe.ci/api.php https://moe.ci/Store/03-808P/0448.jpg
			var url = HttpNet.GetRedirectUrl("https://moe.ci/api.php");
			WebClient client = new WebClient();
			var name_temp = url.Split('/');
			var name = name_temp.Last();
			var path = ImagePath + name;
			if (File.Exists(path))
			{
				return CQ.Code_Image(path.HandleImagePath());
			}
			else
			{
				var data = client.DownloadData(url);
				if (data.Length == 0)
				{
					return "获取失败";
				}
				else
				{
					File.WriteAllBytes(path, data);
					return CQ.Code_Image(path.HandleImagePath());
				}
			}
		}
	}
}
